package com.example.cinema;

import java.util.*;
import java.text.SimpleDateFormat;

class CinemaSystem {
    // 全局数据存储
    public static List<User> users = new ArrayList<>();
    public static List<Movie> movies = new ArrayList<>();
    public static List<Hall> halls = new ArrayList<>();
    public static List<Session> sessions = new ArrayList<>();
    public static List<Ticket> tickets = new ArrayList<>();
    public static Scanner sc = new Scanner(System.in);
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static void main(String[] args) {
        // 初始化数据
        initData();
        System.out.println("=== 万东电影院管理系统 ===");
        login();
    }

    // 初始化默认数据
    private static void initData() {
        // 初始化放映厅
        halls.add(new Hall("黄金厅", 8, 10));
        halls.add(new Hall("白银厅", 10, 12));
        halls.add(new Hall("翡翠厅", 7, 8));
        halls.add(new Hall("钻石厅", 7, 8));
        halls.add(new Hall("宝石厅", 8, 9));

        // 初始化管理员账户
        users.add(new User("U001", "admin", "ynuinfo#777", "管理员", "13800138000", new Date()));
    }

    // 登录功能
    private static void login() {
        System.out.print("请输入用户名：");
        String username = sc.nextLine();
        System.out.print("请输入密码：");
        String password = sc.nextLine();

        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                System.out.println("登录成功！欢迎 " + user.getRole() + "：" + username);
                showMenu(user);
                return;
            }
        }
        System.out.println("用户名或密码错误，请重新登录！");
        login();
    }

    // 显示对应角色菜单
    private static void showMenu(User user) {
        while (true) {
            System.out.println("\n===== 功能菜单 =====");
            switch (user.getRole()) {
                case "管理员":
                    System.out.println("1.用户管理  2.密码修改  3.退出登录");
                    System.out.print("请选择功能：");
                    int adminChoice = sc.nextInt();
                    sc.nextLine(); // 清除缓冲区
                    if (adminChoice == 1) AdminService.userManage();
                    else if (adminChoice == 2) UserService.changePassword(user);
                    else if (adminChoice == 3) {
                        System.out.println("已退出登录");
                        login();
                    }
                    break;
                case "经理":
                    System.out.println("1.影片管理  2.排片管理  3.销售数据  4.退出登录");
                    System.out.print("请选择功能：");
                    int mgrChoice = sc.nextInt();
                    sc.nextLine();
                    if (mgrChoice == 1) ManagerService.movieManage();
                    else if (mgrChoice == 2) ManagerService.sessionManage();
                    else if (mgrChoice == 3) ManagerService.showSales();
                    else if (mgrChoice == 4) {
                        System.out.println("已退出登录");
                        login();
                    }
                    break;
                case "前台":
                    System.out.println("1.查看影片  2.售票  3.退票  4.退出登录");
                    System.out.print("请选择功能：");
                    int frontChoice = sc.nextInt();
                    sc.nextLine();
                    if (frontChoice == 1) FrontService.showMovies();
                    else if (frontChoice == 2) FrontService.sellTicket();
                    else if (frontChoice == 3) FrontService.refundTicket();
                    else if (frontChoice == 4) {
                        System.out.println("已退出登录");
                        login();
                    }
                    break;
            }
        }
    }
}